package com.aplink.generic.widgets;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.database.ISelectCommand;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ControlSpec;

import java.util.ArrayList;
import java.util.HashMap;

public abstract class BaseView {
    private ControlSpec mControlSpec;
    private GenericFragment mDataEntryActivity;
    private HashMap<String, String> mHashPassedValues;
    private GenericObject mPassedGenericObject;

    public GenericObject getPassedGenericObject() {
        return mPassedGenericObject;
    }

    public GenericFragment getDataEntryActivity() {
        return mDataEntryActivity;
    }

    protected ISelectCommand getSelectCommand() {
        return mDataEntryActivity.selectCommand();
    }

    protected HashMap<String, String> getHashPassedValues() {
        return mHashPassedValues;
    }

    public ControlSpec getControlSpec() {
        return mControlSpec;
    }

    public BaseView( GenericFragment genericFragment, ControlSpec controlSpec,
                     View view ) {
        this.mDataEntryActivity = genericFragment;
        this.mControlSpec = controlSpec;
        this.mHashPassedValues = genericFragment.getHashPassedValues();
        this.mPassedGenericObject = genericFragment.getPassedGenericObject();
    }

    protected abstract void loadDataToView();

    protected abstract void doIfNotHashPassedValues();

    protected abstract void setEventListener();

    protected abstract void initViewProperties();

    public abstract GenericAttribute<?, ?> getAttribute();

    protected Context getApplicationContext() {
        return mDataEntryActivity.getApplicationContext();
    }

    protected Activity getActivity() {
        return mDataEntryActivity.getActivity();
    }

    protected OnClickListener getOnViewClickListener() {
        return mDataEntryActivity.getOnViewClickListener();
    }

    protected ArrayList<ObjPropInfo> getObjPropList() {
        return mDataEntryActivity.getObjPropList();
    }

    protected int getContentDataFileId() {
        return mDataEntryActivity.getContentData();
    }

    protected FragmentManager getChildFragmentManager() {
        return mDataEntryActivity.getChildFragmentManager();
    }

    protected FragmentManager getFragmentManager() {
        return mDataEntryActivity.getFragmentManager();
    }

    protected OnItemSelectedListener getOnItemSelectedListener() {
        return mDataEntryActivity.getOnItemSelectedListener();
    }

    public void initView() {
        this.initViewProperties();
        if ( ( this.mHashPassedValues == null )
                || this.mHashPassedValues.isEmpty() ) {
            this.doIfNotHashPassedValues();
        } else {
            this.loadDataToView();
        }
        this.setEventListener();
    }

    protected Resources getResources() {
        return mDataEntryActivity.getResources();
    }

    protected ActionBar getActionBar() {
        return mDataEntryActivity.getActivityActionBar();
    }

    protected View findViewById( int id ) {
        return getActivity().findViewById( id );
    }
}
